﻿#define _CRT_SECURE_NO_WARNINGS



#include <stdio.h>


//循环生成0~9的随机数，但不能和数组arr中已存在的数字重复
#include <stdlib.h>

int main(void)
{
	int i = 0;
	int arr[4] = { 0 };

	for (i = 0; i < 4; i++)
	{
		int j = 0;

		do
		{
			arr[i] = rand() % 10;

			for (j = 0; j < i; j++)
			{
				if (arr[i] == arr[j])
					break;
			}
		} while (j < i);
	}


	return 0;
}

#if 0
//练习 3-8-2 猜赢n次结束
#include <stdlib.h>
#include <time.h>

int cnt = 0;
int i_comp = 1;
int comp1 = 0, comp2 = 0, comp3 = 0, human = 0;
int lose = 0, draw = 0, win = 0;
char* hd[] = { "石头", "剪刀", "布" };

void Init(void)
{
	srand((unsigned)time(NULL));
	printf("要猜赢几次？：");
	scanf("%d", &cnt);

	puts("猜拳游戏开始！！！");
}

void HumanInput(void)
{
	do
	{
		int i = 0;

		printf("\n\a石头剪刀布···");
		for (i = 0; i < sizeof(hd) / sizeof(hd[0]); i++)
			printf(" (%d)%s", i, hd[i]);
		printf("：");
		scanf("%d", &human);
	} while (human < 0 || human > 2);

	comp1 = rand() % 3;
	comp2 = rand() % 3;
	comp3 = rand() % 3;
}

void JudgeAndRecord(int judge)
{
	switch (judge)
	{
	case 0:
		printf("  平局。\n");
		draw++;
		break;
	case 1:
		printf("你输了。\n");
		lose++;
		break;
	case 2:
		printf("你赢了。\n");
		win++;
		break;
	}
}

void Display(int comp)
{
	int judge = 0;

	printf("电脑0""%d""出:%5s，你出:%5s。", i_comp++, hd[comp], hd[human]);
	judge = (human - comp + 3) % 3;

	JudgeAndRecord(judge);
}

int main(void)
{
	Init();

	do
	{
		int judge1 = 0;
		int judge2 = 0;
		int judge3 = 0;

		HumanInput();

		/*printf("电脑0""%d""出:%5s，你出:%5s。", 1, hd[comp1], hd[human]);
		judge1 = (human - comp1 + 3) % 3;
		JudgeAndRecord(judge1);

		printf("电脑0""%d""出:%5s，你出:%5s。", 2, hd[comp2], hd[human]);
		judge2 = (human - comp2 + 3) % 3;
		JudgeAndRecord(judge2);

		printf("电脑0""%d""出:%5s，你出:%5s。", 3, hd[comp3], hd[human]);
		judge3 = (human - comp3 + 3) % 3;
		JudgeAndRecord(judge3);*/
		i_comp = 1;
		Display(comp1);
		Display(comp2);
		Display(comp3);

	} while (win < cnt && lose < cnt);

	printf("\n%d胜%d负%d平。\n", win, lose, draw);

	return 0;
}


//练习 3-7-1 按照玩家意愿重复游戏
#include <stdlib.h>
#include <time.h>

int comp1 = 0, comp2 = 0, human = 0;
int lose = 0, draw = 0, win = 0;
char* hd[] = { "石头", "剪刀", "布" };

void Init(void)
{
	srand((unsigned)time(NULL));
	puts("猜拳游戏开始！！！");
}

void HumanInput(void)
{
	do
	{
		int i = 0;

		printf("\n\a石头剪刀布···");
		for (i = 0; i < sizeof(hd) / sizeof(hd[0]); i++)
			printf(" (%d)%s", i, hd[i]);
		printf("：");
		scanf("%d", &human);
	} while (human < 0 || human > 2);

	comp1 = rand() % 3;
	comp2 = rand() % 3;
}

void JudgeAndRecord(int judge)
{
	switch (judge)
	{
	case 0:
		printf("平局。\n");
		draw++;
		break;
	case 1:
		printf("你输了。\n");
		lose++;
		break;
	case 2:
		printf("你赢了。\n");
		win++;
		break;
	}
}

int Again(void)
{
	int retry = 0;

	do
	{
		printf("再来一局？···（0）否  （1）是：");
		scanf("%d", &retry);
	} while (retry != 0 && retry != 1);//!(retry == 0 || retry == 1)

	return retry;
}

int main(void)
{
	Init();

	do
	{
		int judge1 = 0;
		int judge2 = 0;

		HumanInput();

		printf("电脑01出%s，你出%s。", hd[comp1], hd[human]);
		judge1 = (human - comp1 + 3) % 3;
		JudgeAndRecord(judge1);

		printf("电脑02出%s，你出%s。", hd[comp2], hd[human]);
		judge2 = (human - comp2 + 3) % 3;
		JudgeAndRecord(judge2);

	} while (Again());

	printf("\n%d胜%d负%d平。\n", win, lose, draw);

	return 0;
}

//练习 3-7-2 猜赢n次结束
#include <stdlib.h>
#include <time.h>

int cnt = 0;
int comp1 = 0, comp2 = 0, human = 0;
int lose = 0, draw = 0, win = 0;
char* hd[] = { "石头", "剪刀", "布" };

void Init(void)
{
	srand((unsigned)time(NULL));
	printf("要猜赢几次？：");
	scanf("%d", &cnt);

	puts("猜拳游戏开始！！！");
}

void HumanInput(void)
{
	do
	{
		int i = 0;

		printf("\n\a石头剪刀布···");
		for (i = 0; i < sizeof(hd) / sizeof(hd[0]); i++)
			printf(" (%d)%s", i, hd[i]);
		printf("：");
		scanf("%d", &human);
	} while (human < 0 || human > 2);

	comp1 = rand() % 3;
	comp2 = rand() % 3;
}

void JudgeAndRecord(int judge)
{
	switch (judge)
	{
	case 0:
		printf("平局。\n");
		draw++;
		break;
	case 1:
		printf("你输了。\n");
		lose++;
		break;
	case 2:
		printf("你赢了。\n");
		win++;
		break;
	}
}

int main(void)
{
	Init();

	do
	{
		int judge1 = 0;
		int judge2 = 0;

		HumanInput();

		printf("电脑01出%s，你出%s。", hd[comp1], hd[human]);
		judge1 = (human - comp1 + 3) % 3;
		JudgeAndRecord(judge1);

		printf("电脑02出%s，你出%s。", hd[comp2], hd[human]);
		judge2 = (human - comp2 + 3) % 3;
		JudgeAndRecord(judge2);

	} while (win != cnt && lose != cnt);

	printf("\n%d胜%d负%d平。\n", win, lose, draw);

	return 0;
}


//练习 3-8-1 按照玩家意愿重复游戏
#include <stdlib.h>
#include <time.h>

int comp1 = 0, comp2 = 0, comp3 = 0, human = 0;
int lose = 0, draw = 0, win = 0;
char* hd[] = { "石头", "剪刀", "布" };

void Init(void)
{
	srand((unsigned)time(NULL));
	puts("猜拳游戏开始！！！");
}

void HumanInput(void)
{
	do
	{
		int i = 0;

		printf("\n\a石头剪刀布···");
		for (i = 0; i < sizeof(hd) / sizeof(hd[0]); i++)
			printf(" (%d)%s", i, hd[i]);
		printf("：");
		scanf("%d", &human);
	} while (human < 0 || human > 2);

	comp1 = rand() % 3;
	comp2 = rand() % 3;
	comp3 = rand() % 3;
}

void JudgeAndRecord(int judge)
{
	switch (judge)
	{
	case 0:
		printf("平局。\n");
		draw++;
		break;
	case 1:
		printf("你输了。\n");
		lose++;
		break;
	case 2:
		printf("你赢了。\n");
		win++;
		break;
	}
}

int Again(void)
{
	int retry = 0;

	do
	{
		printf("再来一局？···（0）否  （1）是：");
		scanf("%d", &retry);
	} while (retry != 0 && retry != 1);//!(retry == 0 || retry == 1)

	return retry;
}

int main(void)
{
	Init();

	do
	{
		int judge1 = 0;
		int judge2 = 0;
		int judge3 = 0;

		HumanInput();

		printf("电脑01出%s，你出%s。", hd[comp1], hd[human]);
		judge1 = (human - comp1 + 3) % 3;
		JudgeAndRecord(judge1);

		printf("电脑02出%s，你出%s。", hd[comp2], hd[human]);
		judge2 = (human - comp2 + 3) % 3;
		JudgeAndRecord(judge2);

		printf("电脑03出%s，你出%s。", hd[comp3], hd[human]);
		judge3 = (human - comp3 + 3) % 3;
		JudgeAndRecord(judge3);

	} while (Again());

	printf("\n%d胜%d负%d平。\n", win, lose, draw);

	return 0;
}


//开始
#include <stdlib.h>
#include <time.h>

int comp = 0, human = 0;
int lose = 0, draw = 0, win = 0;
char* hd[] = { "石头", "剪刀", "布" };

void Init(void)
{
	srand((unsigned)time(NULL));
	puts("猜拳游戏开始！！！");
}

void HumanInput(void)
{
	do
	{
		int i = 0;

		printf("\n\a石头剪刀布···");
		for (i = 0; i < sizeof(hd) / sizeof(hd[0]); i++)
			printf(" (%d)%s", i, hd[i]);
		printf("：");
		scanf("%d", &human);
	} while (human < 0 || human > 2);

	comp = rand() % 3;
}

void JudgeAndRecord(int judge)
{
	switch (judge)
	{
	case 0:
		printf("平局。\n");
		draw++;
		break;
	case 1:
		printf("你输了。\n");
		lose++;
		break;
	case 2:
		printf("你赢了。\n");
		win++;
		break;
	}
}

int main(void)
{
	Init();

	do
	{
		int judge = 0;

		HumanInput();

		printf("我出%s，你出%s。\n", hd[comp], hd[human]);

		judge = (human - comp + 3) % 3;

		JudgeAndRecord(judge);

	} while (win < 3 && lose < 3);

	printf(win == 3 ? "\n□你赢了。\n" : "\n■你输了。\n");

	printf("%d胜%d负%d平。\n", win, lose, draw);

	return 0;
}

#endif